package com.haowan123.qinmeiren.appota2;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.net.ConnectivityManager;
import android.os.Environment;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

public class CommonUtils {
	public static String savedPicPath= Environment.getExternalStorageDirectory()+"/SavedImages";
	public static String blurImgPath= Environment.getExternalStorageDirectory()+"/SavedImages/bg.jpg";
	public static String getDeviceId(Context con) {
		return ((TelephonyManager) con.getSystemService(Context.TELEPHONY_SERVICE)).getDeviceId();
	}
	public static String sdCardPath(){
		return Environment.getExternalStorageDirectory().getAbsolutePath();
	}
	
	public static void savePic(Context context,int resId){
		File dest = new File(savedPicPath);
		boolean savable=dest.mkdirs();
		if(!savable&&!dest.exists()){
			Toast.makeText(context, context.getResources().getString(R.string.save_err), Toast.LENGTH_SHORT).show();
			return;
		}
		InputStream in=null;
		OutputStream out=null;
		try{
		in = context.getResources().openRawResource(resId);
		// Used the File-constructor
		out = new FileOutputStream(new File(dest, "pic"+resId+".png"));

		// Transfer bytes from in to out
		byte[] buf = new byte[1024];
		int len;
		    // A little more explicit
		    while ( (len = in.read(buf, 0, buf.length)) != -1){
		         out.write(buf, 0, len);
		    }
		    
		}
		catch (IOException e) {
			e.printStackTrace();
		}finally {
		    // Ensure the Streams are closed:
		    try {
				in.close();
				out.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		Toast.makeText(context, context.getResources().getString(R.string.saved_pic,savedPicPath+"/pic"+resId+".png"), Toast.LENGTH_SHORT).show();
	}
	
	public static boolean checkNetworkAvaliable(Context context) {
		ConnectivityManager cm = (ConnectivityManager) context
				.getSystemService(Context.CONNECTIVITY_SERVICE);

		return cm.getActiveNetworkInfo() != null
				&& cm.getActiveNetworkInfo().isConnectedOrConnecting();
	}
	
	public static int getRelativeLeft(View myView) {
	    if (myView.getParent() == myView.getRootView())
	        return myView.getLeft();
	    else
	        return myView.getLeft() + getRelativeLeft((View) myView.getParent());
	}

	public static int getRelativeTop(View myView) {
	    if (myView.getParent() == myView.getRootView())
	        return myView.getTop();
	    else
	        return myView.getTop() + getRelativeTop((View) myView.getParent());
	}
	
	public static boolean isFileAvaiable(Context context){
		File file=new File(Constants.PATH);
		SharedPreferences shared= context.getSharedPreferences("sys_info", Activity.MODE_PRIVATE);
		long size=shared.getLong("sys_size",100);
		Log.e("", "get file sys size from shared:"+size);
		if(file.exists() && file.length() == size){
			return true;
		}else{
			if(file.exists()){
				//need to del old file & re download
				boolean deleteOldFile=file.delete();
				if(deleteOldFile){
					Log.e("","Delete old file successfully");
				}
			}
			return false;
		}
		
	}
	
	public static boolean deleteOldFile(){
		File file = new File(Constants.PATH);
		return file.delete();
	}
}
